package com.qiniuyun.threedgenerator.config;

import org.apache.catalina.filters.CorsFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

// CorsConfig.java
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许前端域名（根据实际前端地址调整）
        config.addAllowedOrigin("http://localhost:9000"); // Quasar开发服务器
        config.addAllowedOrigin("http://127.0.0.1:9000");
        config.addAllowedOrigin("http://localhost:8080");

        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setAllowCredentials(true); // 允许携带cookie

        source.registerCorsConfiguration("/api/**", config);
        return new CorsFilter();
    }
}